package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class Main {
	
	static boolean checkMate = true; //This will make sure we always have a clean output page rather than a appended one
	
	public static void main(String[] args) throws IOException{
		
		//Path to send output
		String filePath = "E:\\program\\test.html"; //THIS SHOULD BE REMOVED WHEN TESTING IS DONE.
		
		try {
			//data to send over the wire.
			String data = URLEncoder.encode("LoginID", "UTF-8") + "=" + URLEncoder.encode("username", "UTF-8"); //Login Name
			data += "&" + URLEncoder.encode("Password", "UTF-8") + "=" + URLEncoder.encode("password", "UTF-8"); //User Password
			data += "&" + URLEncoder.encode("ClockInOut", "UTF-8") + "=" + URLEncoder.encode("in", "UTF-8"); //Clock in or Clock Out
			data += "&" + URLEncoder.encode("Action", "UTF-8") + "=" + URLEncoder.encode("Submit", "UTF-8"); //Submit our username and password - this doesn't work. There seems to be some type of verification system in place maybe? Need to find out...
			data += "&" + URLEncoder.encode("RedirectTo", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
			data += "&" + URLEncoder.encode("PromptTimes", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8");
			data += "&" + URLEncoder.encode("SubmitTimes", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");
			data += "&" + URLEncoder.encode("PostCheck", "UTF-8") + "=" + URLEncoder.encode("webclock_99.1637680870385_1/30/2010 10:49:17 PM", "UTF-8");
			data += "&" + URLEncoder.encode("postback", "UTF-8") + "=" + URLEncoder.encode("true", "UTF-8");
			
			
			
			//send over wire
			URL url = new URL("https://www.swipeclock.com/sc/clock/webclock.asp");
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write(data);
			wr.flush();
			
			//Get data back from POST
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String HTML = "poop";
			while((HTML = rd.readLine()) != null){
				fileWrite(filePath, HTML);
			}
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
public static void fileWrite(String fileName, String data){
	
	boolean appender = false;

	if(checkMate == false){ appender = true;}

		try{
			FileWriter fstream = new FileWriter(fileName, appender);
			BufferedWriter out = new BufferedWriter(fstream);
			//Write the data
			out.write(data);
			//close the output stream
			out.close();
		}catch(Exception e){
			System.err.println("\nError Writting File!: " + e );
		}
		
		checkMate = false;
	}
	
}
